package com.sgzn.sgpay.persistence.manager;

import com.sgzn.sgpay.enums.SgsResponseCode;
import com.sgzn.sgpay.exception.SgpayException;
import com.sgzn.sgpay.persistence.dao.TLoginSessionMapper;
import com.sgzn.sgpay.persistence.dao.TMchLoginSessionMapper;
import com.sgzn.sgpay.persistence.domain.TLoginSession;
import com.sgzn.sgpay.persistence.domain.TLoginSessionExample;
import com.sgzn.sgpay.persistence.domain.TMchLoginSession;
import com.sgzn.sgpay.persistence.domain.TMchLoginSessionExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author huangsenguang
 * @version Id: MchLoginDaoManager.java, v 0.1 2018/5/19 10:53 huangsenguang Exp $$
 */
@Slf4j
@Component
public class MchLoginDaoManager {
    @Autowired
    private TMchLoginSessionMapper tMchLoginSessionMapper;

    public TMchLoginSession gettLoginSession(String loginToken) throws Exception{
        log.info("loginToken:{}", loginToken);
        TMchLoginSessionExample queryExample = new TMchLoginSessionExample();
        TMchLoginSessionExample.Criteria criteria = queryExample.createCriteria();
        criteria.andTokenEqualTo(loginToken);
        List<TMchLoginSession> sList = tMchLoginSessionMapper.selectByExample(queryExample);
        if(CollectionUtils.isEmpty(sList)){
            throw new SgpayException(SgsResponseCode.C_TOKEN_NOT_FOUND.getResponseCode(),SgsResponseCode.C_TOKEN_NOT_FOUND.getResponseDesc());
        }
        return sList.get(0);
    }
}

